﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Utility;


namespace KorbeRTR
{
    public partial class FormTemperatureSet : Form
    {
        public const int N_Row = 9;
        public const int N_Col = 6;

        private Timer timer;

        public FormTemperatureSet()
        {
            InitializeComponent();

            timer = new Timer();
            timer.Interval = 5000;
            timer.Tick += delegate(object sender, EventArgs e)
            {
                RefreshGrid(grid1);
            };
        }

        private void FormTemperatureSet_Load(object sender, EventArgs e)
        {
            InitGrid(grid1);

            RefreshGrid(grid1);
            timer.Enabled = true;
        }

        /// <summary>
        /// 表格初始化
        /// </summary>
        private void InitGrid(FlexCell.Grid grid)
        {
            string line = ConfigurationManager.AppSettings["LINE"];

            grid.AutoRedraw = false;
            grid.ExtendLastCol = false;

            // 设置行数/列数
            grid.Rows = 10;
            grid.Cols = 7;

            //这个包围区间要减1
            grid.Range(0, 0, grid.Rows - 1, grid.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;

            grid.Column(0).Width = 140;

            // 设置单元格文字
            int col = 0;
            grid.Cell(0, col++).Text = "标签";
            grid.Cell(0, col++).Text = "超温警报";
            grid.Cell(0, col++).Text = "低温警报";
            grid.Cell(0, col++).Text = "设定温度";
            grid.Cell(0, col++).Text = "实际温度";
            grid.Cell(0, col++).Text = "正补偿";
            grid.Cell(0, col++).Text = "负补偿";

            int row = 1;
            grid.Cell(row++, 0).Text = "除油";
            grid.Cell(row++, 0).Text = "热水洗";
            grid.Cell(row++, 0).Text = "整流机冷却";
         //   grid.Cell(row++, 0).Text = "酸洗槽";
            grid.Cell(row++, 0).Text = "镀铜";
            grid.Cell(row++, 0).Text = "6#微蚀";
            //   grid.Cell(row++, 0).Text = "铜槽添加";
          
            grid.Cell(row++, 0).Text = "烘干加热";
            grid.Cell(row++, 0).Text = "6#微蚀";
            grid.Cell(row++, 0).Text = "抗氧化";
            grid.Cell(row++, 0).Text = "热水槽";


            grid.Column(1).CellType = FlexCell.CellTypeEnum.Button;
            grid.Column(2).CellType = FlexCell.CellTypeEnum.Button;
            grid.Column(3).CellType = FlexCell.CellTypeEnum.Button;

            grid.Column(5).CellType = FlexCell.CellTypeEnum.Button;
            grid.Column(6).CellType = FlexCell.CellTypeEnum.Button;

          
            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private void RefreshGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;
            int row = 0;
            for (int i = 0; i < Temperatures.Length; i++)
            {
                var temp = Temperatures[i];

                row = i + 1;

                grid.Cell(row, 1).Text = temp.超温.Value.ToString();
                grid.Cell(row, 1).ForeColor = Color.Black;

                grid.Cell(row, 2).Text = temp.低温.Value.ToString();
                grid.Cell(row, 2).ForeColor = Color.Black;

                grid.Cell(row, 3).Text = temp.设定值.Value.ToString();
                grid.Cell(row, 3).ForeColor = Color.Black;

                grid.Cell(row, 4).Text = temp.实际值.Value.ToString();
                grid.Cell(row, 4).BackColor = temp.BackColor();

                grid.Cell(row, 5).Text = temp.正补偿.Value.ToString();
                grid.Cell(row, 5).ForeColor = Color.Black;

                grid.Cell(row, 6).Text = temp.负补偿.Value.ToString();
                grid.Cell(row, 6).ForeColor = Color.Black;
            }

     

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        public string[] names = { "除油", "热水洗", "整流机冷却",  "镀铜", "微蚀",  "烘干加热", "酸洗", "抗氧化","热水槽" };
        public string[] types = { "超温警报温度", "低温警报温度", "设定温度", "实际温度", "正补偿温度", "负补偿温度" };

        public Models.OmronData.TemperatureParams[] Temperatures =
        {
            FormMainWindow.OmronPLC.CY除油温度,
            FormMainWindow.OmronPLC.RS热水洗温度,
            FormMainWindow.OmronPLC.ZL整流机冷却温度,
      
            FormMainWindow.OmronPLC.DT镀铜温度,
            FormMainWindow.OmronPLC.WS微蚀温度,
       
            FormMainWindow.OmronPLC.HG烘干加热温度,
            FormMainWindow.OmronPLC.SJ酸浸温度,
            FormMainWindow.OmronPLC.KYH抗氧化槽温度,
              FormMainWindow.OmronPLC.RS热水槽温度,
         };

        public string[] MemberName =
        {
            "超温",
            "低温",
            "设定值",
            "实际值",
            "正补偿",
            "负补偿"
        };


        public double[,] MinTable = new double[N_Row, N_Col]
        {
            { 0,0,0,-1,0,0 },
            { 0,0,0,-1,0,0 },
            { 0,0,0,-1,0,0 },
            { 0,0,0,-1,0,0 },
            { 0,0,0,-1,0,0 },
            { 0,0,0,-1,0,0 },
            { 0,0,0,-1,0,0 },
            { 0,0,0,-1,0,0 },
            { 0,0,0,-1,0,0 }
        };
        public double[,] MaxTable = new double[N_Row, N_Col]
        {
            { 15,15,55,-1,55,55 },
            { 15,15,55,-1,55,55 },
            { 15,15,55,-1,55,55 },
      
            { 15,15,55,-1,55,55 },
            { 15,15,75,-1,55,55 },
   
            { 15,15,125,-1,55,55 },
            { 15,15,55,-1,55,55 },
            { 15,15,55,-1,55,55 },
            { 15,15,80,-1,55,55 }
        };



        public int Bound(int min, int x, int max)
        {
            return Math.Max(min, Math.Min(x, max));
        }

        private void grid1_ButtonClick(object Sender, FlexCell.Grid.ButtonClickEventArgs e)
        {
            int row = Bound(0, e.Row - 1, N_Row-1);
            int col = Bound(0, e.Col - 1, N_Col-1);

            //if (e.Row == 9)
            //    return;

            string name = names[row];
            string type = types[col];
            string unit = "摄氏度";
            double min = MinTable[row, col];
            double max = MaxTable[row, col];

            string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

            bool bl = CheckForm(typeof(FormInfo).Name);
            if (!bl)
            {
                FormInfo formInfo = new FormInfo(msg,min,max);
                //   formLogin.MdiParent = this;
                formInfo.ShowDialog();
                if (formInfo.ok)
                {
                    var temp = Temperatures[row];
                    string field = MemberName[col];

                    var tp = temp.GetType();
                    var fi = tp.GetField(field, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                    var m = fi.GetValue(temp) as KorbeRTR.Utility.TemperatureVariable;

                    m.Value = formInfo.input.ToDouble();

                    grid1.Cell(e.Row, e.Col).Text = formInfo.input;
                    grid1.Cell(e.Row, e.Col).ForeColor = Color.Red;
                }
            }
        }

        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshGrid(grid1);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }

    public static partial class KorbeVCPExtersions
    {
        public static Color BackColor(this Models.OmronData.TemperatureParams temp)
        {
            if (temp.实际值.Value > temp.设定值.Value)
            {
                return Color.OrangeRed;
            }

            if (temp.实际值.Value < temp.设定值.Value)
            {
                return Color.DodgerBlue;
            }

            return Color.White;
        }
    }
}
